package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOverviewBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.library.smoothCheckBox.SmoothCheckBox;
import com.zhy.android.percent.support.PercentRelativeLayout;

public class RefundStorageConfirmActivity extends BaseActivity {

    private static final String SUCCESS = "1";//成功

    private TextView title, confirm, pickingNumber, pickingCompany, createDate, totalLine, submitLine, voucherDate, postDate;

    private SmoothCheckBox isFocus;

    private PercentRelativeLayout dateLayout;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_confirm);
        initView(RefundStorageConfirmActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(RefundStorageConfirmActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        confirm = (TextView) activity.findViewById(R.id.confirm);
        pickingNumber = (TextView) activity.findViewById(R.id.pickingNumber);
        pickingCompany = (TextView) activity.findViewById(R.id.pickingCompany);
        createDate = (TextView) activity.findViewById(R.id.createDate);
        totalLine = (TextView) activity.findViewById(R.id.totalLine);
        submitLine = (TextView) activity.findViewById(R.id.submitLine);
        dateLayout = (PercentRelativeLayout) activity.findViewById(R.id.dateLayout);
        voucherDate = (TextView) activity.findViewById(R.id.voucherDate);
        postDate = (TextView) activity.findViewById(R.id.postDate);
        isFocus = (SmoothCheckBox) activity.findViewById(R.id.isFocus);
    }

    private void initData(Activity activity) {
        title.setText("提交");
        confirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                confirm(activity, voucherDate.getText().toString(), postDate.getText().toString());
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        isFocus.setOnCheckedChangeListener((SmoothCheckBox checkBox, boolean isChecked) -> {
            if (isChecked) {
                dateLayout.setVisibility(View.VISIBLE);
                voucherDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                postDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
                voucherDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", voucherDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            voucherDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "voucherDate");
                    }
                });

                postDate.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        ActivityUtils.getTimePickerDialog(activity, "", postDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                            postDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                        }).show(getSupportFragmentManager(), "postDate");
                    }
                });
            } else {
                dateLayout.setVisibility(View.GONE);
            }
        });

        getData(activity);
    }

    private void confirm(Context context, String voucherDate, String postDate) {
        RefundStorageConfirmRequestBean params = RefundStorageUtils.getRefundStorageConfirmRequestBean(context, voucherDate, postDate);
        RefundStorageConfirmRequest.request(context, params, new GsonBeanListener<RefundStorageConfirmBean>() {
            @Override
            public void onSuccess(RefundStorageConfirmBean bean) {
                if (SUCCESS.equals(bean.getData().getCode())) {
                    SPUtils.removeRefundStorageInformation(context);
                    Intent intent = new Intent(context, RefundStorageResultActivity.class);
                    intent.putExtra(RefundStorageExtra.MESSAGE, bean.getData().getMsg());
                    intent.putExtra(RefundStorageExtra.CONFIRM_MESSAGE, "过账成功");
                    startActivity(intent);
                } else {
                    if (!TextUtils.isEmpty(bean.getData().getRefundNumber())) {
                        RefundStorageUtils.updateRefundStorageNumber(context, bean.getData().getRefundNumber());
                    }
                    if (!TextUtils.isEmpty(bean.getData().getMsg())) {
                        Utils.middleToast(context, bean.getData().getMsg());
                    }
                }
            }
        });
    }


    private void getData(Context context) {
        RefundStorageOverviewBean.DataBean data = RefundStorageUtils.getRefundStorageOverviewBean(context).getData();
        pickingNumber.setText(data.getPickingNumber());
        pickingCompany.setText(data.getPickingCompany());
        createDate.setText(data.getCreateDate());
        totalLine.setText(data.getMaterielList().size() + "");
        submitLine.setText(RefundStorageUtils.getRefundStorageNumber(context) + "");
//        LogUtils.i(data.getIsFocus());
//        if (FOCUS.equals(data.getIsFocus())) {
//            dateLayout.setVisibility(View.GONE);
//        } else {
//            dateLayout.setVisibility(View.VISIBLE);
//            voucherDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
//            postDate.setText(Utils.getCurrentStringDate(UtilsConstant.YYYY_MM_DD));
//            voucherDate.setOnClickListener(new NoDoubleClickListener() {
//                @Override
//                public void onNoDoubleClick(View v) {
//                    ActivityUtils.getTimePickerDialog(context, "", voucherDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
//                        voucherDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
//                    }).show(getSupportFragmentManager(), "voucherDate");
//                }
//            });
//
//            postDate.setOnClickListener(new NoDoubleClickListener() {
//                @Override
//                public void onNoDoubleClick(View v) {
//                    ActivityUtils.getTimePickerDialog(context, "", postDate.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
//                        postDate.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
//                    }).show(getSupportFragmentManager(), "postDate");
//                }
//            });
//        }
    }

}
